package com.lzy.crm.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.dao.CrowdMapper;
import com.lzy.crm.dao.CrowdRuleMapper;
import com.lzy.crm.dao.criteria.CrowdRuleCriteria;
import com.lzy.crm.domain.Crowd;
import com.lzy.crm.domain.CrowdRule;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.service.CrowdRuleService;
import com.lzy.crm.service.CrowdUserService;
import com.lzy.crm.service.UserService;

@Service
public class CrowdRuleServiceImpl  extends BaseServiceImpl<CrowdRule, CrowdRuleCriteria> implements CrowdRuleService{

	@Resource
	private CrowdRuleMapper crowdRuleMapper;
	
	
	@Resource
	private CrowdMapper crowdMapper;
	
	@Resource
	private UserService userService;
	
	@Resource
	private CrowdUserService crowdUserService;
	
	@Override
	public BaseMapper<CrowdRule, CrowdRuleCriteria> getBaseMapper() {
		return crowdRuleMapper;
	}

	
	
	/**
	 *  申请加入群方法
	 * @param crowdId
	 * @param remark
	 */
	@Override
	public int addRuleCrowd(int crowdId,String remark,String rule){
		Crowd crowd = crowdMapper.getById(crowdId);
		if(crowd==null){
			return 0;
		}

		// 群主信息
		CrowdRule crowdRule =new CrowdRule();
		crowdRule.setCrowdId(crowd.getId());
		crowdRule.setStatus("1");
		crowdRule.setCreateTime(new Date());
		crowdRule.setRemark(remark);
		crowdRule.setRule(rule);
		crowdRuleMapper.insertSelective(crowdRule);
		return 1;
	}
	


	
	/**
	 * 修改申请信息
	 * @param crowdRuleId
	 * @param status
	 * @param remark
	 * @return
	 * @throws Exception 
	 */
	@Override
	@Transactional
	public int updateCrowdRule(String crowdRuleId,String status,String remark,PpUser pUser) throws Exception{
		CrowdRule crowdRule = crowdRuleMapper.getById(crowdRuleId);

		crowdRule.setUpdateTime(new Date());
		crowdRuleMapper.updateByPrimaryKeySelective(crowdRule);
		return 1;
	}
	
	/**
	 * @param crowdId
	 * @param remark
	 */
	@Override
	@Transactional
	public List<CrowdRule> getRuleList(int crowdId){
		return crowdRuleMapper.getCrowdRuleListByCrowdId(crowdId);
	}

	/**
	 *  查询群申请的消息数量
	 * @param crowdId
	 * @param remark
	 */
	@Override
	@Transactional
	public CrowdRule getRule(int ruleId){
		return crowdRuleMapper.getById(ruleId);
	}

	
	
}
